﻿'@Tác giả : 
'@Tên file: FrmDivision.vb
'@Ngày tạo: 
'@Ngày sửa: 
'@Mục đích: Thực hiện các chức năng thêm mới, sửa và xóa danh mục đơn vị
'***********************************************************************
Imports System
Imports Microsoft.VisualBasic
Imports System.Linq
Imports System.Windows.Forms
Imports Publics


Public Class FrmCapNhatDV

    Private _close As Boolean = False
    Private _IntID_DonViCha As String
    Private _ObjDonVi As DM_DonVi
    Public Property ObjDonVi() As DM_DonVi
        Get
            Return Me._ObjDonVi
        End Get
        Set(ByVal value As DM_DonVi)
            Me._ObjDonVi = value
        End Set
    End Property

    Private _IDDonVi As String = ""
    Public Property IDDonVi() As String
        Get
            Return _IDDonVi
        End Get
        Set(ByVal value As String)
            _IDDonVi = value
        End Set
    End Property

    Private _ActionForm As String
    Public Property ActionForm() As String
        Get
            Return _ActionForm
        End Get
        Set(ByVal value As String)
            _ActionForm = value
        End Set
    End Property

    Private Sub iButtonChonDonViCha_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnChonDonViCha.Click, BtnBoChon.Click
        Dim Frm As New FrmChonDonVi
        Frm.vDataSource = From ds In dbTV.DM_DonVis _
                                      Where ds.InUsed _
                                      Order By ds.ThuTu _
                                      Select ds
        Frm.ShowDialog()
        If Frm.ObjDonVi IsNot Nothing Then
            Me.TxtTenDonViCha.Text = Frm.ObjDonVi.TenDonVi
            Me._IntID_DonViCha = Frm.ObjDonVi.ID
        End If
    End Sub

    Private Sub iFrmDivision_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If e.KeyChar = Chr(Windows.Forms.Keys.Enter) Then
            System.Windows.Forms.SendKeys.Send("{tab}")
        End If
    End Sub
    Private Sub GetCaptionForm()
        Dim strGhiVaDong As String = String.Empty
        Dim strThoat As String = GetStrKey(kThoat)

        If BtnGhiVaDong.Visibility = DevExpress.XtraBars.BarItemVisibility.Always And BtnGhiVaDong.Enabled = True Then
            strGhiVaDong = GetStrKey(kGhiVaDong)
        End If

        'If btnTroGiup.Visibility = DevExpress.XtraBars.BarItemVisibility.Always And btnTroGiup.Enabled = True Then
        '    strTroGiup = GetStrKey(kTroGiup)
        'End If
        Me.Text = Me.Text + strGhiVaDong + strThoat 'strTroGiup + 
    End Sub
    Private Sub iFrmDivision_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        BtnGhiVaDong.Caption = BtnSaveAndCloseCaption
        BtnHuyBo.Caption = BtnCancelCaption
        BtnTroGiup.Caption = BtnHelpCaption
        Me.TxtMa.Focus()
        Select Case ActionForm
            Case vAddAction
                Me.Text = "Cập nhật đơn vị"
            Case vCopyAction
                Me.Text = "Cập nhật đơn vị"
                If Not Me.ObjDonVi Is Nothing Then
                    Me.TxtMa.Text = ObjDonVi.MaDonVi

                    Me.TxtTen.Text = ObjDonVi.TenDonVi
                    If ObjDonVi.ParentID IsNot Nothing Then
                        Me._IntID_DonViCha = CStr(ObjDonVi.ParentID)
                    End If
                    Dim iParentDivision As DM_DonVi = (From ds In dbTV.DM_DonVis _
                                                       Where ds.ID = ObjDonVi.ParentID _
                                                       Select ds).FirstOrDefault()
                    If iParentDivision IsNot Nothing Then
                        Me.TxtTenDonViCha.Text = iParentDivision.TenDonVi
                    End If
                    Me.TxtDiaChi.Text = ObjDonVi.DiaChi
                    
                    Me.TxtThuTu.Text = CStr(ObjDonVi.ThuTu)
                    ActionForm = vAddAction
                End If
            Case vEditAction
                Me.Text = "Cập nhật đơn vị"
                If Not Me.ObjDonVi Is Nothing Then
                    Me.TxtMa.Text = ObjDonVi.MaDonVi

                    Me.TxtTen.Text = ObjDonVi.TenDonVi
                    If ObjDonVi.ParentID IsNot Nothing Then
                        Me._IntID_DonViCha = CStr(ObjDonVi.ParentID)
                    End If
                    Dim iParentDivision As DM_DonVi = (From ds In dbTV.DM_DonVis _
                                       Where ds.ID = ObjDonVi.ParentID _
                                       Select ds).FirstOrDefault()
                    If iParentDivision IsNot Nothing Then
                        Me.TxtTenDonViCha.Text = iParentDivision.TenDonVi
                    End If
                    Me.TxtDiaChi.Text = ObjDonVi.DiaChi
                    'Dim strMaKho As String = ""
                    'Dim strMaNhom As String = ""
                    'If Not String.IsNullOrEmpty(ObjDonVi.IDKhos) Then
                    '    For Each item In ObjDonVi.IDKhos.Split(",")
                    '        strMaKho &= (From ds In dbTV.DM_Khos Where ds.ID = item Select ds.MaKho).FirstOrDefault & ","
                    '    Next
                    'End If
                    'If Not String.IsNullOrEmpty(ObjDonVi.IDNhomKhachHangs) Then
                    '    For Each item In ObjDonVi.IDNhomKhachHangs.Split(",")
                    '        strMaNhom &= (From ds In dbTV.DM_NhomKhachHangs Where ds.ID = item Select ds.MaNhom).FirstOrDefault & ","
                    '    Next
                    'End If
                    'If strMaKho <> "" Then
                    '    txtListKhoHienThi.Text = strMaKho.Substring(0, strMaKho.Length - 1)
                    'Else
                    '    txtListKhoHienThi.Text = ""
                    'End If

                    'If strMaNhom <> "" Then
                    '    txtMaNhomDViHienThi.Text = strMaNhom.Substring(0, strMaNhom.Length - 1)
                    'Else
                    '    txtMaNhomDViHienThi.Text = ""
                    'End If
                End If
        End Select
        GetCaptionForm()
    End Sub

    'Kiểm tra nhập liệu, trả lại xâu báo lỗi
    Private Function CheckSave() As String
        If Me.TxtTenDonViCha.Text = "" Then
            _IntID_DonViCha = Nothing
        End If
        Dim st As String = ""
        If Trim(Me.TxtMa.Text) = "" Then
            st = "Chưa nhập mã đơn vị"
            Me.TxtMa.Focus()
        ElseIf Trim(Me.TxtTen.Text) = "" Then
            st = "Chưa nhập tên đơn vị"
            Me.TxtTen.Focus()
        ElseIf ActionForm = vEditAction Then
            If Me.ObjDonVi IsNot Nothing Then
                If Me.ObjDonVi.ID = _IntID_DonViCha Then
                    st = "Không chọn đơn vị cấp trên là chính mình"
                    Me.TxtTenDonViCha.Focus()
                Else
                    Dim iTemp As DM_DonVi = (From ds In dbTV.DM_DonVis _
                                       Where ds.InUsed AndAlso ds.MaDonVi = Trim(Me.TxtMa.Text) _
                                       AndAlso ds.ID <> Me.ObjDonVi.ID _
                                       Select ds).FirstOrDefault()
                    If iTemp IsNot Nothing Then
                        st = "Trùng đơn vị"
                    End If
                End If
            End If
        ElseIf ActionForm = vAddAction Then
            Dim iTemp As DM_DonVi = (From ds In dbTV.DM_DonVis _
                                       Where ds.InUsed AndAlso ds.MaDonVi = Trim(Me.TxtMa.Text) _
                                       Select ds).FirstOrDefault()
            If iTemp IsNot Nothing Then
                st = "Trùng mã đơn vị"
            End If
        End If
        CheckSave = st
    End Function


    Private Sub iTxtTenDonViCha_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtTenDonViCha.KeyDown
        If e.KeyCode = Windows.Forms.Keys.Enter Then
            iButtonChonDonViCha_Click(Nothing, Nothing)
        End If
    End Sub


    Private Sub FrmCapNhatDV_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        'Ghi và đóng
        If (e.Modifiers = GetKeyModifide(pIdGhiVaDong) And _
            e.KeyCode = GetKeyCode2(pIdGhiVaDong)) Or _
            (e.KeyCode = GetKeyCode(pIdGhiVaDong)) Then

            If Me.BtnGhiVaDong.Enabled = True And Me.BtnGhiVaDong.Visibility = DevExpress.XtraBars.BarItemVisibility.Always Then
                BtnGhiVaDong_ItemClick(Nothing, Nothing)
            End If

        End If
        'Huỷ bỏ
        If (e.Modifiers = GetKeyModifide(pIdThoat) And _
            e.KeyCode = GetKeyCode2(pIdThoat)) Or _
            (e.KeyCode = GetKeyCode(pIdThoat)) Then

            If Me.BtnHuyBo.Enabled = True And Me.BtnHuyBo.Visibility = DevExpress.XtraBars.BarItemVisibility.Always Then
                BtnHuyBo_ItemClick(Nothing, Nothing)
            End If

        End If
        'Trợ giúp
        If (e.Modifiers = GetKeyModifide(pIdTroGiup) And _
           e.KeyCode = GetKeyCode2(pIdTroGiup)) Or _
           (e.KeyCode = GetKeyCode(pIdTroGiup)) Then

            If Me.BtnTroGiup.Enabled = True And Me.BtnTroGiup.Visibility = DevExpress.XtraBars.BarItemVisibility.Always Then
                BtnTroGiup_ItemClick(Nothing, Nothing)
            End If


        End If
    End Sub

    Private Sub BtnGhiVaDong_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnGhiVaDong.ItemClick
        Dim sThongBao As String = CheckSave()
        If sThongBao <> "" Then
            MsgErr(sThongBao)
            _close = False
            Exit Sub
        End If
        Try
            Select Case ActionForm
                Case vEditAction
                    Dim iEditDivision As DM_DonVi = (From ds In dbTV.DM_DonVis _
                                                   Where ds.ID = Me.ObjDonVi.ID _
                                                   Select ds).FirstOrDefault()
                    If iEditDivision IsNot Nothing Then
                        IDDonVi = iEditDivision.ID
                        iEditDivision.MaDonVi = Trim(Me.TxtMa.Text)

                        iEditDivision.TenDonVi = Trim(Me.TxtTen.Text)
                        iEditDivision.ParentID = Me._IntID_DonViCha
                        iEditDivision.DiaChi = Trim(Me.TxtDiaChi.Text)
                        iEditDivision.ThuTu = CInt(Me.TxtThuTu.Text)
                        iEditDivision.EditedOn = CDate(dbTV.GetDateTimeServer())
                        'Sửa trường DaGui của các đơn vị cấp trên
                        If iEditDivision.ParentID IsNot Nothing Then
                            Dim StrID_DONVICHA As String = CStr(iEditDivision.ParentID)
                            While StrID_DONVICHA <> ""
                                Dim ObjDonViCha As DM_DonVi = (From ds In dbTV.DM_DonVis Where ds.ID = StrID_DONVICHA Select ds).FirstOrDefault()
                                If ObjDonViCha IsNot Nothing Then

                                    If ObjDonViCha.ParentID IsNot Nothing Then
                                        StrID_DONVICHA = CStr(ObjDonViCha.ParentID)
                                    Else
                                        StrID_DONVICHA = Nothing
                                    End If
                                Else
                                    StrID_DONVICHA = Nothing
                                End If
                            End While
                        End If
                    End If
                Case vAddAction
                    Dim ObjDonVi As New DM_DonVi
                    ObjDonVi.ID = CreateID()
                    IDDonVi = ObjDonVi.ID
                    ObjDonVi.MaDonVi = Trim(Me.TxtMa.Text)

                    ObjDonVi.TenDonVi = Trim(Me.TxtTen.Text)
                    ObjDonVi.DiaChi = Trim(Me.TxtDiaChi.Text)
                    
                    ObjDonVi.ParentID = Me._IntID_DonViCha
                    ObjDonVi.ThuTu = CInt(Me.TxtThuTu.Text)
                     
                    ObjDonVi.InUsed = True

                    ObjDonVi.EditedOn = CDate(dbTV.GetDateTimeServer)
                    dbTV.DM_DonVis.InsertOnSubmit(ObjDonVi)
                    'Sửa trường DaGui của các đơn vị cấp trên
                    If ObjDonVi.ParentID IsNot Nothing Then
                        Dim StrID_DONVICHA As String = CStr(ObjDonVi.ParentID)
                        While StrID_DONVICHA <> ""
                            Dim ObjDonViCha As DM_DonVi = (From ds In dbTV.DM_DonVis Where ds.ID = StrID_DONVICHA Select ds).FirstOrDefault()
                            If ObjDonViCha IsNot Nothing Then

                                If ObjDonViCha.ParentID IsNot Nothing Then
                                    StrID_DONVICHA = CStr(ObjDonViCha.ParentID)
                                Else
                                    StrID_DONVICHA = Nothing
                                End If
                            Else
                                StrID_DONVICHA = Nothing
                            End If
                        End While
                    End If
            End Select
            iSubmitChange() ' Lưu bản ghi trước khi get ID_Childs cho từng thằng 
            'Tạo dữ liệu ID_Childs
            For Each ObjDonVi As DM_DonVi In (From ds In dbTV.DM_DonVis Where ds.InUsed Select ds)
                Dim sID As String = ObjDonVi.ID
                ObjDonVi.ID_Childs = ConvertCollectionToString(iGetAllDivisionChild(sID))
            Next
            iSubmitChange()
            _close = True
            Me.Close()
        Catch ex As Exception
            MsgErr(ex.Message)
        End Try
    End Sub

    Private Sub BtnHuyBo_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnHuyBo.ItemClick
        Me.Close()
    End Sub

   
    Private Sub btnChonKhoBDG_NL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChonKhoBDG_NL.Click
        'Dim frmKho As New FrmChonDMKho
        'frmKho.lstMaChon = txtListKhoHienThi.Text.Trim.Split(",").ToList
        'frmKho.ShowDialog()
        'If frmKho.lstMaChon.Count > 0 Then
        '    txtListKhoHienThi.Text = String.Join(",", (From ds In frmKho.lstMaChon Order By ds Select ds).ToArray)
        '    txtListKhoHienThi.Tag = String.Join(",", (From ds In frmKho.lstIDchon Order By ds Select ds).ToArray)
        'Else
        '    txtListKhoHienThi.Text = ""
        'End If
    End Sub

    Private Sub btnMaNhomDViKhongHienThiTrenPhatHanh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChonNhom.Click
        'Dim Frm As New FrmChonDMNhomKhachHang
        'Frm.lstMaChon = txtMaNhomDViHienThi.Text.Trim.Split(",").ToList
        'Frm.ShowDialog()
        'If Frm.lstMaChon.Count > 0 Then
        '    txtMaNhomDViHienThi.Text = String.Join(",", (From ds In Frm.lstMaChon Order By ds Select ds).ToArray)
        '    txtMaNhomDViHienThi.Tag = String.Join(",", (From ds In Frm.lstIDChon Order By ds Select ds).ToArray)
        'Else
        '    txtMaNhomDViHienThi.Text = ""
        '    txtMaNhomDViHienThi.Tag = Nothing
        'End If
    End Sub

    Private Sub BtnTroGiup_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnTroGiup.ItemClick

    End Sub
End Class